package objects;

import java.io.Serializable;
import kernel.DataObstacle;
import kernel.DataType;
import results.Traceable;

import java.util.*;
import utils.*;

/**
 * @author alemaitr
 *
 * <em>Note : </em> this class need to provide an xml description of itself.
 * We could  have made Obstacle extend Dumpable.
 * As we want the application flexible, we have chosen Traceable
 * in the case we'll need to monitor the obstacles, for instance (nb of mobilestation
 * that have entered into collision)
 */
public class Obstacle extends SimObject  implements Serializable,Traceable {

    private Shape shape; 	//la forme de l'obstacle
    private double alpha;	//le taux de ralentissement
    private double gamma;   //le taux d'attenuation
    private double width;	//largeur de l'obstacle
    private double height;	//hauteur de l'obstacle
    private String name;
    private String xmlKey;
    private static int cpt=0;
	

    public void init(){
	DataObstacle data = new DataObstacle();
	setProperties(data);
	if(name == ""){
	    name = "Obstacle"+cpt;
	    xmlKey = "obstacle_" + cpt;
	    cpt++;
	}
    }

    /**
     * Build a new Obstacle. In default, there's neither slowing down nor attenuation
     * @param s the shape of the obstacle
     */
    public Obstacle(Shape s){
	init();
	shape = s;
	width = s.getWidth();
	height = s.getHeight();
    }

    /**
     * @return the horizontal coordonate of the left upper corner of the obstacle
     */
    public double getX() {
	return shape.getX();
    }

    /**
     * @return the vertical coordonate of the left upper corner of the obstacle
     */
    public double getY() {
	return shape.getY();
    }

    /**
     * @return the max width of the obstacle
     */
    public double getWidth() {
	return width;
    }

    /**
     * @return the max height of the obstacle 
     */
    public double getHeight() {
	return height;
    }
	
    /**
     * @return the slowing-down rate of the obstacle
     */
    public double getAlpha(){
	return alpha;
    }
	
    /**
     * @return the attenuation rate of the obstacle
     */
    public double getGamma(){
	return gamma;
    }
	
    /**
     * 
     * @return the shape of the obstacle
     */
    public Shape getShape()
    {
	return shape;
    }
	
    /**
     * 
     * @param newAlpha the new value of the slowing down rate
     */
    public boolean setAlpha(double newAlpha){
	if(newAlpha<=1 && newAlpha >=0){
		
	    alpha=newAlpha;
	    return true;
	}else{
	    return false;
	}
    }
	
    /**
     * 
     * @param newGamma the new value of the slowing down rate
     */
    public boolean setGamma(double newGamma){
	if(newGamma<=1 && newGamma >=0){
	    gamma=newGamma;
	    return true;
	}else{
	    return false;
	}
    }
	
    public static void main(String[] args) {
    }

    /**
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     * @return the lenght of superposition of the vector(x1,y1,x2,y2) and this obstacle
     */
    public double getLenghtSuperpositionOfVector(double x1, double y1, double x2, double y2) {
	return shape.getLenghtSuperpositionOfVector(x1,y1,x2,y2);
    }
	
    /* (non-Javadoc)
     * @see Objects.SimObject#setProperties(Kernel.DataType)
     */
    public boolean setProperties(DataType data) {
	this.name=((DataObstacle)data).getName();
	boolean a = this.setAlpha(((DataObstacle)data).getAlpha());
	boolean b = this.setGamma(((DataObstacle)data).getGamma());
	return a && b;
    }

    /* (non-Javadoc)
     * @see Objects.SimObject#getProperties()
     */
    public DataType getProperties() {
	DataType d = new DataObstacle(this.name,this.alpha,this.gamma);
	return d;
    }

    public String getName(){
	return name;
    }

    /**
     *  (non-Javadoc)
     * @see Results.Traceable#xmlId()
     */
    public String xmlId() {
		
	return xmlKey;
    }

    /** 
     * (non-Javadoc)
     * @see Results.Dumpable#xmlDump()
     */
    public String xmlDump() {
	HashMap<String,Object> attr = new HashMap<String,Object>();
	attr.put("name",getName());
	attr.put("brakingRate",getAlpha());
	attr.put("waveBrakingRate",getGamma());
	attr.put("uuid",getUUID());

	StringBuffer s = new StringBuffer("");
	s.append(XMLHelper.XMLOpenTag("obstacle",attr));
	s.append(getShape().xmlDump());
	s.append(XMLHelper.XMLCloseTag("obstacle"));

	return s.toString();
	
    }

    public String toString()
    {
	return name;
    }

}
